package com.example.gustavo.hello.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Point;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Vibrator;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.gustavo.hello.R;
import com.example.gustavo.hello.view.SlidingTabLayout;
import com.viewpagerindicator.CirclePageIndicator;
import com.viewpagerindicator.LinePageIndicator;
import com.viewpagerindicator.TabPageIndicator;
import com.viewpagerindicator.TitlePageIndicator;
import com.viewpagerindicator.UnderlinePageIndicator;

import java.util.ArrayList;
import java.util.List;

/**
 * 自定义SlidingTabLayout
 */
public class SlidingTabActivity extends FragmentActivity implements SensorEventListener{

    private static final String TAG = "SlidingTabActivity";
    private SensorManager sm;
    private Vibrator vibrator;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sliding_tab);

        vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
        sm = (SensorManager) getSystemService(SENSOR_SERVICE);

        SlidingTabLayout slidingTabLayout = (SlidingTabLayout) findViewById(R.id.stl);
        ViewPager viewPager = (ViewPager) findViewById(R.id.vp);
//        TitlePageIndicator titlePageIndicator = (TitlePageIndicator) findViewById(R.id.titles);
        LinePageIndicator pageIndicator = (LinePageIndicator) findViewById(R.id.indicator);
        UnderlinePageIndicator titleIndicator = (UnderlinePageIndicator) findViewById(R.id.titleIndicator);


        /*ArrayList<Fragment> lists = new ArrayList<Fragment>();
        lists.add(PlaceholderFragment.newInstance(1));
        lists.add(PlaceholderFragment.newInstance(2));
        lists.add(PlaceholderFragment.newInstance(3));
        MyViewpagerAdapter adapter = new MyViewpagerAdapter(getSupportFragmentManager(),lists);*/


        final List<Integer> viewList = new ArrayList<>();
        viewList.add(R.drawable.ic_drawer);
        viewList.add(R.drawable.ic_launcher);
        viewList.add(R.drawable.bg);
        PagerAdapter adapter = new PagerAdapter() {
            @Override
            public int getCount() {
                return viewList.size() * 4;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                ImageView imageView = new ImageView(SlidingTabActivity.this);
                imageView.setImageResource(viewList.get(position % viewList.size()));
                container.addView(imageView);

                return imageView;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {


                    container.removeView((View) object);

            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return "title" + position;
            }
        };

        viewPager.setAdapter(adapter);

//        titleIndicator.setViewPager(viewPager);
//        pageIndicator.setViewPager(viewPager);

        slidingTabLayout.setCustomTabView(R.layout.tab_title, R.id.tv);
        slidingTabLayout.setViewPager(viewPager);


    }

    @Override
    protected void onResume() {
        super.onResume();
        if (sm != null) {
            sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_NORMAL);
        }



    }

    @Override
    protected void onPause() {
        super.onPause();
        if (sm != null) {
            sm.unregisterListener(this);
        }
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        int type = event.sensor.getType();
        float[] values = event.values;
        if (type == Sensor.TYPE_ACCELEROMETER) {

            Log.i(TAG,"x="+values[0]+" y="+values[1]+ " z="+values[2]);
            if ((Math.abs(values[0]) > 17 || Math.abs(values[1]) > 17 || Math
                    .abs(values[2]) > 17))
            {
                Log.d("sensor x ", "============ values[0] = " + values[0]);
                Log.d("sensor y ", "============ values[1] = " + values[1]);
                Log.d("sensor z ", "============ values[2] = " + values[2]);
//                tv.setText("摇一摇成功!!!");
                //摇动手机后，再伴随震动提示~~
                vibrator.vibrate(500);
            }
        }
    }
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        //传感器精度变化
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_sliding_tab, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }



    class MyViewpagerAdapter extends FragmentPagerAdapter {
        ArrayList<Fragment> fragments;

        public MyViewpagerAdapter(FragmentManager fm,ArrayList<Fragment> dataList) {
            super(fm);
            fragments = dataList;
        }



        @Override
        public Fragment getItem(int i) {
            return fragments.get(i);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return "tab_"+position;
        }
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private static final String ARG_SECTION_NUMBER = "section_number";

        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_my, container, false);

            TextView textView = (TextView) rootView.findViewById(R.id.section_label);
            Bundle arguments = getArguments();
            if(arguments != null){
                textView.setText("tab_"+arguments.getInt(ARG_SECTION_NUMBER));
            }

            return rootView;
        }

    }
}
